56. PowerShell Script Analyzer

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

PSScriptAnalyzer, https://github.com/PowerShell/PSScriptAnalyzer, is a static code checker for Windows PowerShell modules and scripts. PSScriptAnalyzer checks the quality of Windows PowerShell code by running a set of rules based on PowerShell best practices identified by the PowerShell Team and community. It generates DiagnosticResults (errors and warnings) to inform users about potential code defects and suggests possible solutions for improvements.

1
Install-Module -Name PSScriptAnalyzer

56.1: Analyzing scripts with the built-in preset rulesets

ScriptAnalyzer ships with sets of built-in preset rules that can be used to analyze scripts. These include: PSGallery,

DSC and CodeFormatting. They can be executed as follows:

PowerShell Gallery rules

To execute the PowerShell Gallery rules use the following command:

1
Invoke-ScriptAnalyzer -Path /path/to/module/ - Settings PSGallery -Recurse

DSC rules

To execute the DSC rules use the following command:

1
Invoke-ScriptAnalyzer -Path /path/to/module/ - Settings DSC -Recurse

Code formatting rules

To execute the code formatting rules use the following command:

1
Invoke-ScriptAnalyzer -Path /path/to/module/ - Settings CodeFormatting -Recurse

56.2: Analyzing scripts against every built-in rule

To run the script analyzer against a single script file execute:

1
Invoke-ScriptAnalyzer -Path myscript.ps1

This will analyze your script against every built-in rule. If your script is sufficiently large that could result in a lot of warnings and/or errors.

To run the script analyzer against a whole directory, specify the folder containing the script, module and DSC files you want analyzed. Specify the Recurse parameter if you also want sub-directories searched for files to analyze.

1
Invoke-ScriptAnalyzer -Path. -Recurse

56.3: List all built-in rules

To see all the built-in rules execute:

1
Get-ScriptAnalyzerRule